/**
 * Created by taohj on 2014/5/21.
 * 
 */

var role = new Object();


/**
 * 模块列表
 * */
role.initgrid = function () {

    
    role.datagrid = $("#sys_role_list_datagrid");
    
    
    
    role.datagrid.datagrid({
        url: functions.get("SysRoleList"),
        singleSelect: true,
        collapsible: true,
        pagination: true,
        rownumbers: true,
        fitColumns:true,
        sortName:"id",
        sortOrder:"asc",
        fit: true,
        pageList: [20, 50, 100],
        pageSize: 20,
        onDblClickRow:function(){
            role.datagrid.datagrid("options").toolbar[2].handler();
        },
        toolbar: [

            "->",
            {
                iconCls: 'icon-add',
                text: 'Create',
                handler: function () {
                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: 'SysRoleCreate',
                        href: functions.get("SysRoleCreateView"),
                        modal: true,
                        width: 350
                    });
                    

                }
            },
            '-',
            {
                iconCls: 'icon-edit',
                text: 'Modify',
                handler: function () {
                    var selectRow= role.datagrid.datagrid("getSelected");
                    if(!selectRow){
                        //todo
                        return;
                    }

                    var id="";
                    if(selectRow){
                        id=selectRow.id;
                    }

                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: 'SysRoleModifyView',
                        href: functions.get('SysRoleModifyView')+'?id='+id,
                        modal: true,
                        width: 350
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                text: 'Remove',
                handler: function () {

                    var selectRow= role.datagrid.datagrid("getSelected");
                    if(!selectRow){
                        //todo
                        return;
                    }

                    $.messager.confirm('Confirm', 'Are you sure remove selected object?', function(r){
                        if (r){
                            $.post(functions.get('SysRoleRemove'), {
                                id:selectRow.id
                            }, function (result) {
                                util.requestUtil(result,null,role.datagrid);
                            });
                        }
                    });
                    console.info(selectRow);
                }
            },'-',{
                iconCls:'icon-edit',
                text:'Authorize',
                handler:function(){

                    var selectRow= role.datagrid.datagrid("getSelected");
                    if(!selectRow){
                        //todo
                        return;
                    }

                   var dialog=$("<div></div>");
                   dialog.dialog({
                       title:selectRow.name+'-->Authorize',
                       href: functions.get('SysRoleAuthorizeView')+'?id='+selectRow.id,
                       modal: true,
                       width: 350,
                       height:400,
                       top:50,
                       buttons:[{
                           text:'Save',
                           handler:function(){
                               var nodes =  role.authorizeTreegrid.tree('getChecked', ['checked','indeterminate']);

                               //todo
                               var params={'id':selectRow.id};
                               var p="?";
                               for(var i=0;i<nodes.length;i++){
                                  p+='mid='+nodes[i].id;
                                   if(i<nodes.length-1){
                                       p+='&';
                                   }
                               }


                               $.post(functions.get('SysRoleAuthorize')+p, params, function (result) {
                                   util.requestUtil(result,dialog,null);
                               });


                           }
                       },{
                           text:'Close',
                           handler:function(){
                               dialog.dialog('close');
                           }
                       }]


                   })
                }
            }
        ],
        columns:[[
            {field:"id",title:"ID",width:60,sortable:true},
            {field:"name",title:"NAME",width:200,sortable:true},
            {field:"description",title:"DESCRIPTION"}
        ]]
    });
};




//
role.authorizeInit=function(id){


    role.authorizeTreegrid = $("#sys_role_authorize_tree");

    role.authorizeTreegrid.tree({
        url: functions.get("SysRoleAuthorizeList")+"?id="+id,
        singleSelect: true,
        checkbox:true,
        fit: true,
        animate: true

    });


}


//新建提交
role.createSubmit = function (button) {
    var form =util.form(button);
    var params = util.serializeObject(form);
    if (util.form(button).form("validate")) {
        $.post(functions.get('SysRoleCreate'), params, function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result,dialog,role.datagrid)
        });
    }
};


//修改提交
role.modifySubmit = function (button) {
    var form =util.form(button);
    var params = util.serializeObject(form);
    if (util.form(button).form("validate")) {
        $.post(functions.get('SysRoleModify'), params, function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result,dialog,role.datagrid);
        });
    }
};


//搜索
role.searchSubmit=function(button){
    var form =util.form(button);
    var params = util.serializeObject(form);
    role.datagrid.datagrid("load",params);
}







